﻿using System.Collections.Generic;
using System.Data;
using MediSIIM.BizComponent.HangZhouYHQYBInterface;

namespace MediSIIM.BizComponent.HangZhouYHQYB
{
    public class HangZhouYHQYBTradeParms
    {
        public HangZhouYHQYBTradeParms()
        {

        }
        public List<string> FailTables { get; set; }
        /// <summary>
        /// 设置基本入参
        /// </summary>
        /// <param name="index">随便传什么</param>
        /// <returns></returns>
        public ParamRow this[int index] { get { return ParamInfo; } }
        /// <summary>
        /// 基本入参对象
        /// </summary>
        public ParamRow ParamInfo { get; set; }
        /// <summary>
        /// 设置或获取数据集入参,不存在时返回null
        /// </summary>
        /// <param name="name">数据集名称</param>
        /// <returns></returns>
        public ParamTable this[string name]
        {
            get { return InputTable.ContainsKey(name) ? InputTable[name.ToString()] : null; }
            set { InputTables.Add(name); value.TableName = name; InputTable[name.ToString()] = value; }
        }
        /// <summary>
        /// 获得出参数据集
        /// </summary>
        /// <param name="name">数据集名称</param>
        /// <param name="index">随意传</param>
        /// <returns></returns>
        public ParamTable this[string name, int index]
        {
            get { return OutputTable.ContainsKey(name) ? OutputTable[name] : null; }
        }

        /// <summary>
        /// 获取入参信息
        /// </summary>
        /// <returns></returns>
        public string GetInputParam()
        {
            var str = string.Empty;
            foreach (var item in ParamInfo.Columns)
            {
                str += string.Format("{0}={1}|", item.Name, item.Value);
            }
            return str;
        }
        /// <summary>
        /// 数据集入参集合
        /// </summary>
        public Dictionary<string, ParamTable> InputTable { get; set; }
        public List<string> InputTables { get; set; }
        /// <summary>
        /// 数据集出参集合
        /// </summary>
        public Dictionary<string, ParamTable> OutputTable { get; set; }
        /// <summary>
        /// 出参数据集列表
        /// </summary>
        public List<string> OutTables { get; set; }
        public HangZhouYHQYBTradeParms(int tradeNo)
        {
            TradeNo = tradeNo;
            InputTables = new List<string>();
            InputTable = new Dictionary<string, ParamTable>();
            OutputTable = new Dictionary<string, ParamTable>();
            ParamInfo = ParamTable.NewParamRow();
            OutTables = new List<string>();
            FailTables = new List<string>();
        }

        public void SetOutResult(params string[] args)
        {
            OutTables.Clear();
            OutTables.AddRange(args);
        }

        public void SetFailOutResult(params string[] args)
        {
            FailTables.Clear();
            FailTables.AddRange(args);
        }

        public int TradeNo;

        /// <summary>
        /// 获取数据集,不为空而且有行才返回true
        /// </summary>
        /// <param name="table"></param>
        /// <param name="name">数据集名称</param>
        /// <returns></returns>
        public bool GetOutTable(out ParamTable table, string name)
        {
            table = this[name, 0];
            return table != null && table.Rows.Count > 0;
        }

        /// <summary>
        /// 交易执行结果
        /// </summary>
        public int RetCode { get; set; }
    }

    public class ParamRow
    {
        public string this[string name]
        {
            get { return Values.ContainsKey(name) ? Values[name] : ""; }
            set
            {
                ShowData.Rows.Add(name, value);
                Columns.Add(KeyValue.Create(name, value));
                Values.Add(name, value);
            }
        }
        public DataTable ShowData { get; set; }
        public KeyValue this[int index] { get { return Columns[index]; } }
        public List<KeyValue> Columns { get; set; }
        public Dictionary<string, string> Values { get; set; }
        public ParamRow()
        {
            Values = new Dictionary<string, string>();
            Columns = new List<KeyValue>();
            ShowData = new DataTable();
            ShowData.Columns.Add("name");
            ShowData.Columns.Add("value");
        }
    }

    public class ParamTable
    {
        public DataTable TableView { get; set; }
        public ParamRow this[int index] { get { return Rows[index]; } }
        public string TableName { get; set; }
        public List<ParamRow> Rows { get; set; }
        public ParamTable()
        {
            Rows = new List<ParamRow>();
        }

        public static ParamTable NewTable()
        {
            return new ParamTable();
        }

        /// <summary>
        /// 创建新的数据集参数
        /// </summary>
        /// <param name="name">数据集名称</param>
        /// <returns></returns>
        public static ParamTable NewTable(string name)
        {
            return new ParamTable { TableName = name, Rows = new List<ParamRow>() };
        }
        public void AddRow(ParamRow row)
        {
            //TODO:表视图
            if (TableView == null)
            {
                TableView = new DataTable(TableName);
                foreach (var item in row.Columns)
                {
                    TableView.Columns.Add(item.Name);
                }
            }
            Rows.Add(row);
            var newrow = TableView.NewRow();
            foreach (var item in row.Columns)
            {
                newrow[item.Name] = item.Value;
            }
            TableView.Rows.Add(newrow);
        }
        public static ParamRow NewParamRow()
        {
            return new ParamRow();
        }
    }
}
